package com.application.Activity;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.ArrayList;

import ReadWriteXML.CMember;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;
import com.application.DTO.AccountDTO;
import com.application.Network.Protocol;
import com.application.PhanQuyen.Quyen;
import com.application.QuickAction.ActionItem;
import com.application.QuickAction.QuickAction;


public class trucActivity extends Activity {
	public static int CODE_KICK = 101;
	public static int CODE_CHATPEER = 401;
	public static String IP_CHATPEER = "IP_CHATPEER";
	public static int CODE_CHAT = 201;
	public static int CODE_STOP_TALK = 301;
	public static String IP_RESULT = "IP_RESULT";

	int port = Protocol.PORT;
//	public static ArrayList<Integer> yesVote;
//	public static ArrayList<Integer> noVote;

	public static Runnable showMessage;
	public static Runnable showChatMessage;
//	public static Runnable dislayKickMember;
//	public static Runnable dislayKickedMessage;
	public static Runnable showResultVoteKick;
	public static Runnable finishAct;
	public static Runnable showForm1;
	public static Runnable showFormChatPeer;
	public static Runnable showFormChatPeerNonParamater;
	public static Runnable showFormConversation;
	//public static int timeVoteKick;
//	int timeStopTalk;
	public static String message;
	public static String addressFriend;// dia chi cua nguoi ban chat rieng voi minh
	public static String chatMessage;

	//chat rieng
	public static String ipSender;
	public static String userNameSender;
	//end chat rieng
//	public static Address ipBeingKick;
//	public static Address ipBeingStopTalk;

	//public static int kickVoteResult;
	public ArrayList<Quyen> quyen = null;
	public static AccountDTO account = null;
	public EditText listChat;
	public EditText txtContent;
	public static Context baseContext;
	public static Handler handler = new Handler();
	public static Runnable raShowMessage;
	public static Runnable raSignout;
	public static Runnable raStopSession;
	public static Runnable raShowDanhGia;
	public static Runnable raShowBieuQuyet;
	static String loi = "";
	String ip = "";
	ArrayList<CMember> memberList;
	public static int idindex = 0;
	public static Address Address;
	public static String _title = "";

	private static AlertDialog.Builder _builder;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dung_noi_chuyen);
		Log.d("Phu", "Vao oncreate");
		// ip = NetworkController._peer.getAddressPeer();
		listChat = (EditText) findViewById(R.id.txtListMessage);
		memberList = ReadWriteFileController.readMember(ReadWriteFileController.MEMBER_PATH);
		
		showMessage = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Show(message);
			}
		};
		showFormChatPeer = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				ShowChatPeerForm(ipSender,userNameSender);
			}
		};
		
		showFormChatPeerNonParamater=new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
			}
		};
		showFormConversation=new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				ShowFormConversation();
			}
		};
		showChatMessage = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				CapNhatListChat(chatMessage, addressFriend);
			}
		};
//		dislayKickMember = new Runnable() {
//
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				HienThiMHBieuQuyetKick();
//			}
//		};
//
//		dislayKickedMessage = new Runnable() {
//
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				ShowMessageKicked();
//			}
//		};

		finishAct = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				finishActivity();
			}
		};
		
//		showResultVoteKick = new Runnable() {
//
//			@Override
//			public void run() {
//				ShowResultVoteKick();
//			}
//		};
		

		

		txtContent = (EditText) findViewById(R.id.txtMessage);
		txtContent.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_ENTER) {
					sendChatGroup();
				}
				return false;
			}
		});
		Button btnSendMessage = (Button) findViewById(R.id.btnSendMessage);
		btnSendMessage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				sendChatGroup();
				
			}
		});

		
		// -----------------An

		// Khai báo & bắt sự kiện Onclick Button Sign Out khỏi Nhóm
		Button btn_SignOut = (Button) findViewById(R.id.btnSignOut);
		btn_SignOut.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ArrayList<CMember> cmem = ReadWriteFileController.readMember(ReadWriteFileController.MEMBER_PATH);
		        for(int i=0; i<cmem.size(); i++){
		        	String name = String.valueOf(NetworkController._peer.getAccount().get_Username());
		        	Log.d("PEERDROID","fullname: "+ name);
		        	NetworkController.sendGroupSignOut(cmem.get(i).get_ip(),name);
		        }
		        
		        //ghi xuống file Log
		        String text = "Đã rời khỏi nhóm..";
		        ReadWriteFileController.checkRuleWriteToLogFile(text);
		        
				trucActivity.this.finish();
			}
		});

		// Runable raShowMessage lỗi
		raShowMessage = new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				showMessage(loi);
			}
		};

		// Runable raSignOut
		raSignout = new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Log.d("Phu", "vao 'raSignout'");
				showMessageSignOut(loi);
			}
		};

		// Khai báo & bắt sự kiện Onclick Button Yêu cầu Đánh giá
		// "Bản thân & người khác"
		Button btnDanhGia = (Button) findViewById(R.id.btnDanhGia);
		btnDanhGia.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Quyen quyen = NetworkController._peer.getAccount().getRuleById(19);
				Log.d("PEERDROID","Quyen send Đánh giá = " + quyen.getSend());
				if (quyen != null && quyen.getSend()) {
					ArrayList<CMember> cmem = ReadWriteFileController.readMember(ReadWriteFileController.MEMBER_PATH);
			        for(int i=0; i<cmem.size(); i++){
			        	Log.d("PEERDROID","IP memberlist: "+String.valueOf(cmem.get(i).get_ip()));
			        	NetworkController.sendGroupDanhGia(cmem.get(i).get_ip());
			        	//NetworkController.sendGroupDanhGia("192.168.1.99:" + port);
			        }
			        
			        //ghi xuống file Log
			        String text = "Gởi yêu cầu Đánh giá..";
			        ReadWriteFileController.checkRuleWriteToLogFile(text);
				}else{
					Show(Protocol.NON_RULE_MESSAGE);
				}	
			}
		});

		// Runalbe raShowDanhGia
		raShowDanhGia = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				// Hien thong bao yeu cau nguoi khac Danh gia
				displayNotificationDanhGia();
			}
		};

		// Khai báo & bắt sự kiện Onclick Button Xem Đánh Giá
		Button btnviewdanhgia = (Button) findViewById(R.id.btnViewDanhGia);
		btnviewdanhgia.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(trucActivity.this, frmResultDanhGiaActivity.class);
				startActivity(i);
				
				//ghi xuống file Log
				String text = "Xem kết quả Đánh giá..";
				ReadWriteFileController.checkRuleWriteToLogFile(text);
			}
		});
		
		
		// Các khai báo QuickAction
		// vote100accept action item
		ActionItem vote100accept = new ActionItem();

		vote100accept.setTitle("100% Accept");
		vote100accept.setIcon(getResources().getDrawable(R.drawable.ic_add));

		// vote75accept action item
		ActionItem vote75accept = new ActionItem();

		vote75accept.setTitle("75% Accept");
		vote75accept.setIcon(getResources().getDrawable(R.drawable.ic_accept));

		// vote50accept action item
		ActionItem vote50accept = new ActionItem();

		vote50accept.setTitle("50% Accept");
		vote50accept.setIcon(getResources().getDrawable(R.drawable.ic_up));

		final QuickAction mQuickAction = new QuickAction(this);
		// add QuickAction
		mQuickAction.addActionItem(vote100accept);
		mQuickAction.addActionItem(vote75accept);
		mQuickAction.addActionItem(vote50accept);

		// setup the action item click listener
		mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
			@Override
			public void onItemClick(int pos) {
				if (pos == 0) { // Add item selected
					showDialogQuickAction(1);
				} else if (pos == 1) { // Accept item selected
					showDialogQuickAction(2);
				} else if (pos == 2) { // Upload item selected
					showDialogQuickAction(3);
				}
			}
		});

		// Khai báo & bắt sự kiện OnClick Button Biểu Quyết - hiển thị ra
		// QuickAction
		Button btnbieuquyet = (Button) findViewById(R.id.btnBieuQuyet);
		btnbieuquyet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Quyen quyen = NetworkController._peer.getAccount().getRuleById(18);
				Log.d("PEERDROID","Quyen send = " + quyen.getSend());
				if (quyen != null && quyen.getSend()) {
					mQuickAction.show(v);
					mQuickAction.setAnimStyle(QuickAction.ANIM_GROW_FROM_CENTER);
				}else{
					Log.d("PEERDROID","ko co quyen send bieu quyet ");
					Show(Protocol.NON_RULE_MESSAGE);
				}	
			}
		});

		// Runable raShowBieuQuyet
		raShowBieuQuyet = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				// hien thong bao yeu cau Bieu Quyet
				displayNotificationBieuQuyet();
			}
		};

		// Khai báo & bắt sự kiện Onclick Button Xem Biểu Quyết
		Button btnviewbieuquyet = (Button) findViewById(R.id.btnViewBieuQuyet);
		btnviewbieuquyet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(trucActivity.this, frmResultBieuQuyetActivity.class);
				startActivity(i);
				
				//ghi xuống file Log
				String text = "Xem Kết quả Biểu Quyết..";
		        //WriteToLogFile(text);
				ReadWriteFileController.checkRuleWriteToLogFile(text);
			}
		});
		
		Button btnXemFileLog = (Button)findViewById(R.id.btnViewLogFile);
		btnXemFileLog.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(trucActivity.this, frmViewLogFile.class);
				startActivity(i);
				
			}
		});
	}

	// **************************************
	//
	// -----Ham con - SUB FUNCTION----
	//
	// **************************************

//	public void ShowSetTimeVoteKick(final String IP) {
//		final EditText txtTime = new EditText(this);
//		txtTime.setSingleLine();
//		txtTime.setInputType(2);
//		AlertDialog dlg = new AlertDialog.Builder(this).setTitle("Chọn thời gian biểu quyết")
//				.setMessage("Thời gian được tính bằng giây.").setView(txtTime)
//				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//
//					@Override
//					public void onClick(DialogInterface dialog, int which) {
//						// TODO Auto-generated method stub
//						String StrTime = txtTime.getText().toString();
//						int time = Integer.parseInt(StrTime);
//						timeVoteKick = time;
//						yesVote = new ArrayList<Integer>();
//						noVote = new ArrayList<Integer>();
//						ipBeingKick = new Address(IP);
//
//						for (CMember member : memberList) {
//							NetworkController.SendVoteKickMember(new Address(member.get_ip()), ipBeingKick, time);
//						}
//						Log.d("Phu", "Da gui xong kick");
//						// dem thoi gian
//						long kickTime = timeVoteKick * 1000;
//
//						CountDownTimer timer = new CountDownTimer(kickTime, 1000) {
//							@Override
//							public void onTick(long millisUntilFinished) {
//
//							}
//
//							@Override
//							public void onFinish() {
//								kickVoteResult = 0;
//								if (yesVote.size() != 0 || noVote.size() != 0) {
//									if (yesVote.size() >= noVote.size()) {
//										kickVoteResult = 1;
//									}
//									for (CMember member : memberList) {
//										Log.d("Phu", "gui toi " + member.get_ip());
//										NetworkController.SendResultVoteKickMember(new Address(member.get_ip()),
//												kickVoteResult, ipBeingKick);
//									}
//								} else {
//									NetworkController.ShowMessageNotice("Không có ai tham gia biểu quyết.");
//									NetworkController.SendResultVoteKickMember(new Address(ipBeingKick),
//											kickVoteResult, ipBeingKick);
//								}
//							}
//						}.start();
//					}
//				}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
//
//					@Override
//					public void onClick(DialogInterface dialog, int which) {
//						// TODO Auto-generated method stub
//
//					}
//				}).create();
//		dlg.show();
//
//	}

//	public void ShowMessageKicked() {
//		// Toast.makeText(this, "hehe Xin chao", Toast.LENGTH_SHORT).show();
//		AlertDialog dlg = new AlertDialog.Builder(this).setTitle("Thông báo").setMessage("Bạn đã bị mời ra khỏi nhóm.")
//				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//
//					@Override
//					public void onClick(DialogInterface dialog, int which) {
//						// TODO Auto-generated method stub
//						NetworkController.CloseDiscussActivity();
//					}
//				}).create();
//		dlg.show();
//	}

//	public void ShowSetTimeStopTalking(final String ipStop) {
//		final EditText txtTimeTalk = new EditText(this);
//		txtTimeTalk.setSingleLine();
//		txtTimeTalk.setInputType(2);
//		AlertDialog dlg = new AlertDialog.Builder(this).setTitle("Chọn thời gian buộc ngừng thảo luận")
//				.setMessage("Thời gian được tính bằng giây.").setView(txtTimeTalk)
//				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//
//					@Override
//					public void onClick(DialogInterface dialog, int which) {
//						// TODO Auto-generated method stub
//						String StrTime = txtTimeTalk.getText().toString();
//						int time = Integer.parseInt(StrTime);
//						timeStopTalk = time;
//						ipBeingStopTalk = new Address(ipStop);
//						NetworkController.GuiYeuCauNgungThaoLuan(ipBeingStopTalk, time);
//						Log.d("Phu", "vao onclick ngung noi");
//						long timeLong = timeStopTalk * 1000;
//						Toast.makeText(getBaseContext(), timeLong + "", 1).show();
//						CountDownTimer timer = new CountDownTimer(timeLong, 1000) {
//
//							@Override
//							public void onTick(long millisUntilFinished) {
//								// TODO Auto-generated method stub
//
//							}
//
//							@Override
//							public void onFinish() {
//								// TODO Auto-generated method stub
//								Toast.makeText(getBaseContext(), "Het gio", 1).show();
//								NetworkController.GuiChoPhepThaoLuan(ipBeingStopTalk);
//							}
//						}.start();
//					}
//
//				}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
//
//					@Override
//					public void onClick(DialogInterface dialog, int which) {
//						// TODO Auto-generated method stub
//
//					}
//				}).create();
//		dlg.show();
//	}

	@Override
	public void onStart() {
		super.onStart();	
//		Log.d("Phu", "vao truc activity onstart");
//		if (NetworkController._peer == null) {
//
//			Log.d("Phu", "_peer == null");
//			account = new AccountDTO();
//		//	account.set_Username("ptttruc");
//			account.set_Username("phu");
//			NetworkController.init("MES", port, account);
//			NetworkController.resetRole();
//			this.setTitle(NetworkController._peer.getAddressPeer());
//		}
////		// NetworkController.resetRole();
//		for (int i = 0; i < NetworkController._peer.getAccount().getRule().size(); i++) {
//
//			Log.d("Phu", "rule " + NetworkController._peer.getAccount().getRule().get(i).toString());
//		}
	}

	public void Show(String message) {
	
		AlertDialog dlg = new AlertDialog.Builder(this).setTitle("Thông báo").setMessage(message)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				}).create();
		dlg.show();
	}

	// ham nay da bo di rui ko xai nua
	public void ShowResultVoteKick() {
		// Log.d("Phu", "ShowResultVoteKick");
		// if (yesVote.size() > 0 || noVote.size() > 0) {
		// Log.d("Phu", "ShowResultVoteKick YES");
		// String message = "Kết quả biểu quyết : ";
		// // message+="\n yes : "+yesVote.size();
		// // message+=" no : "+noVote.size()+"\n";
		// int sizeALL = yesVote.size() + noVote.size();
		// float yesPer = (float) yesVote.size() / sizeALL * 100;
		// float noPer = (float) noVote.size() / sizeALL * 100;
		// int yesPercent, noPercent;
		// yesPercent = (int) yesPer;
		// noPercent = (int) noPer;
		// // message+="\n All: "+sizeALL+
		// // ", yesPer="+yesPercent+", noPer="+noPercent;
		// message += yesPercent + "% đồng ý và " + noPercent +
		// "% không đồng ý.\n";
		// if (yesPercent >= noPercent) {
		// kickVoteResult = 1;// bi moi ra khoi nhom
		// message += "Kết quả cuối cùng là: YES";
		// } else {
		// kickVoteResult = 0;// khong bi moi ra khoi nhom
		// message += "Kết quả cuối cùng là: NO";
		// }
		// message += "\n Gửi kết quả tới các thành viên trong nhóm?";
		// AlertDialog dlg = new
		// AlertDialog.Builder(this).setTitle("Thông báo kết quả biểu quyết")
		// .setMessage(message).setPositiveButton("OK", new
		// DialogInterface.OnClickListener() {
		//
		// @Override
		// public void onClick(DialogInterface dialog, int which) {
		// ArrayList<CMember> memberList = ReadWriteFileController
		// .readMember(ReadWriteFileController.MEMBER_PATH);
		// for (CMember member : memberList) {
		// Log.d("Phu", "gui toi " + member.get_ip());
		// NetworkController.SendResultVoteKickMember(new
		// Address(member.get_ip()),
		// kickVoteResult, ipBeingKick);
		// }
		// }
		// }).setNegativeButton("NO", new DialogInterface.OnClickListener() {
		//
		// @Override
		// public void onClick(DialogInterface dialog, int which) {
		// // TODO Auto-generated method stub
		//
		// }
		// }).create();
		// dlg.show();
		// } else {
		// // NetworkController._peer.getAccount().getRuleById(id)
		// Log.d("Phu", "ShowResultVoteKick NO");
		// NetworkController.ShowMessageNotice("Không có ai tham gia biểu quyết.");
		// NetworkController.SendResultVoteKickMember(new Address(ipBeingKick),
		// kickVoteResult, ipBeingKick);
		// }
	}

	public void CapNhatListChat(String mess, String add) {
		String s = listChat.getText().toString();
		s += "\n" + add + " :" + mess;
		// s+=NetworkController._peer.getAccount().getRule().get(0).getSend();
		listChat.setText(s);

	}

//	public void HienThiMHBieuQuyetKick() {
//		Log.d("Phu", "vao ham HienThiMHBieuQuyetKick");
//		Intent i = new Intent(this.getBaseContext(), frmBieuQuyetMoiRoiNhom.class);
//		startActivity(i);
//	}

	
	public void finishActivity() {
		finish();
	}

	public void ShowChatPeerForm(String ipReceiver,String userName) {
//		Intent i = new Intent(this, frmChatPeerActivity.class);
//		startActivity(i);
		Intent data=new Intent(this,frmChatPeerActivity.class);
		data.putExtra(frmChatPeerActivity.IP_CHAT_PEER, ipReceiver);
		data.putExtra(frmChatPeerActivity.USERNAME, userName);
		setResult(frmChatPeerActivity.CODE_CHATPEER, data);
		
		
		startActivity(data);
	}
	
	public void ShowChatPeerFormNonParamater() {
		Intent i = new Intent(this, frmChatPeerActivity.class);
		startActivity(i);
		
	}
	
	public void ShowFormConversation() {
		Intent i = new Intent(this, frmMyConversation.class);
		startActivity(i);
	}

	//******************
	// -----------An
	//******************
	public static void show(String message) {
		loi = message;
		handler.post(raShowMessage);

	}

	public void showMessage(String message) {
		Log.d("PEERDROID", "toi show message");
		AlertDialog alert = new AlertDialog.Builder(this).create();
		alert.setMessage(message);
		alert.setTitle("test");
		alert.show();
	}
	
	public static void showSignOut(String name) {
		loi = name;
		Log.d("PEERDROID", "name sign out: " + loi);
		handler.post(raSignout);

	}
	public void showMessageSignOut(String name) {
		Log.d("PEERDROID", "vao day");
		Toast.makeText(this, name + " da roi nhom", Toast.LENGTH_SHORT).show();
	}

	public void displayNotificationDanhGia() {
		Intent intent = new Intent(trucActivity.this, frmDanhGiaActivity.class);
		Bundle ex = new Bundle();
		ex.putInt("ID", 2);
		ex.putString("SENDER", String.valueOf(Address));
		intent.putExtras(ex);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
		NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notif = new Notification(R.drawable.ic_launcher, "Co yeu cau Danh gia", System.currentTimeMillis());
		notif.setLatestEventInfo(this, "Danh gia ", "Vao de", pendingIntent);

		notiManager.notify(2, notif);
		Log.d("PEERDROID", "het show notifi danh gia");
	}

	public void displayNotificationBieuQuyet() {
		Intent intent = new Intent(this, frmBieuQuyetActivity.class);
		Bundle ex = new Bundle();
		ex.putInt("ID", 3);
		ex.putInt("INDEX", idindex);
		ex.putString("SENDER", String.valueOf(Address));
		ex.putString("TITLE", _title);
		intent.putExtras(ex);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
		NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notif = new Notification(R.drawable.ic_launcher, "Co yeu cau Bieu Quyet",
				System.currentTimeMillis());
		notif.setLatestEventInfo(this, "Bieu quyet ", "Vao de", pendingIntent);

		notiManager.notify(3, notif);
		Log.d("PEERDROID", "het show notifi bieu quyet");
	}

	public static void showNotificationBieuQuyet(int index, Address sender,String title) {
		Log.d("PEERDROID", " da vao shownotifi......");
		idindex = index;
		Address = sender;
		_title = title;
		Log.d("PEERDROID", "vao frm truc" + String.valueOf(index) + String.valueOf(sender));
		handler.post(raShowBieuQuyet);
	}
	
	public static void showRequestDanhGia( Address sender) {
		Log.d("PEERDROID", " da vao shownotifi......");
		Address = sender;
		Log.d("PEERDROID", "vao frm truc" + String.valueOf(sender));
		handler.post(raShowDanhGia);
	}
	
	//Hàm Show Dialog khi click chọn QuickAction
	public void showDialogQuickAction(final int index){
		final EditText txtTitle = new EditText(this);
		txtTitle.setInputType(1);
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("Yêu Cầu Biểu Quyết")
			.setMessage("Nhập vào vấn đề muốn biểu quyết")
			.setView(txtTitle)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					String strTitle = txtTitle.getText().toString();
					ArrayList<CMember> cmem = ReadWriteFileController.readMember(ReadWriteFileController.MEMBER_PATH);
			        for(int i=0; i<cmem.size(); i++){
			        	NetworkController.sendGroupBieuQuyet(cmem.get(i).get_ip(), index, strTitle);
			        }
			        //Ghi xuống file Log
			        String text = "Đã gởi yêu cầu Biểu Quyết..";
			        //checkIsThanhLapNhom(text);
			        ReadWriteFileController.checkRuleWriteToLogFile(text);
				}
			})
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();
				}
			}).create();
		dialog.show();
	}
	

	//**************
	//----End An
	//**************

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		new MenuInflater(getApplication()).inflate(R.menu.omnu_thao_luan, menu);
//		return (super.onCreateOptionsMenu(menu));
//	}
//
//	@Override
//	public boolean onOptionsItemSelected(MenuItem item) {
//		int id = item.getItemId();
//		switch (id) {
//		case R.id.mnuDanhSachThanhVien:
//			Intent iDanhSachThanhVien = new Intent(getBaseContext(), frmDanhSachThanhVienActivity.class);
//			startActivityForResult(iDanhSachThanhVien, CODE_KICK);
//			break;
//		case R.id.mnuChiaSeTaiLieu:
//			Toast.makeText(getBaseContext(), "Chia sẻ tài liệu", 1).show();
//			break;
//		default:
//			break;
//		}
//		return true;
//	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.d("Phu"," vao ham onActivityResult");
		Log.d("Phu", "resultCode= "+resultCode);
//		if (resultCode == CODE_KICK) {
//			Log.d("Phu"," CODE_KICK");
//			String ip = data.getStringExtra(IP_RESULT);
//			ShowSetTimeVoteKick(ip);
//		}
//		if (resultCode == CODE_CHATPEER) {
//			Log.d("Phu"," CODE_CHATPEER");
//			String ipFriend = data.getStringExtra(IP_CHATPEER);
//			if (ipFriend != null) {
//				
//				NetworkController.ShowChatPeerNonParamater();
//			}
//		}
//		if (resultCode == CODE_STOP_TALK) {
//			Log.d("Phu"," CODE_STOP_TALK");
//			String ipStop = data.getStringExtra(IP_RESULT);
//			if (ipStop != null) {
//
//			//	ShowSetTimeStopTalking(ipStop);
//			}
//		}
	}

	private void sendChatGroup() {
		Quyen quyen = NetworkController._peer.getAccount().getRuleById(4);
		if (quyen != null && quyen.getSend()) {
			ArrayList<CMember> memberList = ReadWriteFileController.readMember(ReadWriteFileController.MEMBER_PATH);

			for (CMember member : memberList) {
				NetworkController.GuiTinNhanChat(new Address(member.get_ip()), txtContent.getText().toString());
			}
			//ghi xuống file Log
	        String text = "Chat: " + txtContent.getText().toString();
	        //checkIsThanhLapNhom(text);
	        ReadWriteFileController.checkRuleWriteToLogFile(text);
			txtContent.setText("");
		} else {
			Show(Protocol.NON_RULE_MESSAGE);
		}

	}
}
